package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.domain.SjContract;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.gcgl.service.ISjContractService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 合同审批Controller
 * 
 * @author ruoyi
 * @date 2025-07-22
 */
@Controller
@RequestMapping("/sj/contract")
public class SjContractController extends BaseController
{
    private String prefix = "sj/contract";

    @Autowired
    private ISjContractService sjContractService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysRoleService roleService;
    
    final String processCode = "sjcontract";
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    

    @RequiresPermissions("system:contract:view")
    @GetMapping()
    public String contract()
    {
        return prefix + "/contract";
    }
    
    /**
     * 查询列表
     */
    @RequiresPermissions("system:contract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjContract sjContract)
    {
        sjContract.setContractType(0);
        sjContract.setDeleteFlag(1);
        
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	sjContract.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		sjContract.setOwnerId(getUserId());
            }
        }
        //----
        startPage();
        List<SjContract> list = sjContractService.selectSjContractList(sjContract);
        
        for(SjContract obj : list){
        	if(obj.getAuthority()==null || obj.getAuthority().toString().equals("")){
        		obj.setAuthorityName("");
        	}else{
        		SysDept dept = deptService.selectDeptById(Long.parseLong(obj.getAuthority().toString()));
            	obj.setAuthorityName(dept.getDeptName());
        	}
        }
        
        return getDataTable(list);
    }


    /**
     * 导出
     */
    @RequiresPermissions("system:contract:export")
    @Log(title = "合同审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjContract sjContract)
    {
    	sjContract.setContractType(0);
    	sjContract.setDeleteFlag(1);
        List<SjContract> list = sjContractService.selectSjContractList(sjContract);
        ExcelUtil<SjContract> util = new ExcelUtil<SjContract>(SjContract.class);
        return util.exportExcel(list, "项目主合同数据");
    }
    
    /**
     * 新增
     */
    @RequiresPermissions("system:contract:add")
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList4Approve(new SysUser()));
        
        GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        gcProject.setStatus(3);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        mmap.put("orderNo", ReturnUUID.getOrderNo("HT"));
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
        
        mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/add";
    }


    /**
     * 新增
     */
    @RequiresPermissions("system:contract:add")
    @Log(title = "合同审批", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(SjContract sjContract) {
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());
    		sjContract.setCustomerId(Integer.parseInt(gcProject.getCustomerId().toString()));
    		sjContract.setCustomerName(gcProject.getCustomerName());
    	}
    	
    	String htje = sjContract.getHtje().toString();
    	sjContract.setContractType(0);//客户合同
    	sjContract.setHtjedx(ConvertUpMoneyUtil.toChinese(htje));
    	sjContract.setOwner(getLoginName());
    	sjContract.setStatus(1);
    	sjContract.setDeleteFlag(1);
    	sjContract.setCreateAt(new Date());
    	sjContract.setCreateUser(getLoginName());
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.insertSjContract4Save(sjContract));
    }
    
    /**
     * 提交
     */
    @RequiresPermissions("system:contract:add")
    @Log(title = "合同审批", businessType = BusinessType.INSERT)
    @PostMapping("/addSubmit")
    @ResponseBody
    public AjaxResult addSubmit(SjContract sjContract) {
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());
    		sjContract.setCustomerId(Integer.parseInt(gcProject.getCustomerId().toString()));
    		sjContract.setCustomerName(gcProject.getCustomerName());
    		
    		gcProject.setZongje(sjContract.getHtje().doubleValue());
        	gcProjectService.updateGcProject(gcProject);
    	}
    	
    	String htje = sjContract.getHtje().toString();
    	sjContract.setContractType(0);//客户合同
    	sjContract.setHtjedx(ConvertUpMoneyUtil.toChinese(htje));
    	sjContract.setOwner(getLoginName());
    	sjContract.setStatus(1);
    	sjContract.setDeleteFlag(1);
    	sjContract.setCreateAt(new Date());
    	sjContract.setCreateUser(getLoginName());
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.insertSjContract(sjContract));
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        gcProject.setStatus(3);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList4Approve(new SysUser()));
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
        //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/edit";
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjContract sjContract)
    {
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());
    		sjContract.setCustomerId(Integer.parseInt(gcProject.getCustomerId().toString()));
    		sjContract.setCustomerName(gcProject.getCustomerName());
    	}
    	
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract4Save(sjContract));
    }
    
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/editSubmit")
    @ResponseBody
    public AjaxResult editSubmit(SjContract sjContract)
    {
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());
    		sjContract.setCustomerId(Integer.parseInt(gcProject.getCustomerId().toString()));
    		sjContract.setCustomerName(gcProject.getCustomerName());
    	
    		gcProject.setZongje(sjContract.getHtje().doubleValue());
        	gcProjectService.updateGcProject(gcProject);
    	}
    	
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract(sjContract));
    }
    
    /**
     * 详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        mmap.put("sjContract", sjContract);
        return prefix + "/detail";
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:contract:remove")
    @Log(title = "合同审批", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjContractService.deleteSjContractByIds(ids));
    }
    
    //---------------------审批方法
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
		
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        return configlis;
    }
    
    /**
     * 修改保存项目立项
     */
    @Log(title = "更新", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(SjContract sjContract){
    	sjContract.setUpdateUser(getLoginName());
    	sjContract.setUpdateAt(new Date());
        return toAjax(sjContractService.updateSjContract4Save(sjContract));
    }
    
    /**
     * 运营中心
     * @return
     */
    @GetMapping("/yyzx")
    public String yyzx1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
            
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getSbbmshId());
            mmap.put("approvename", obj.getSbbmsh());
        }
        return prefix + "/yyzxcheck";
    }
    
    /**
     * 申请部门
     * @return
     */
    @GetMapping("/dept")
    public String dept2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
            
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getXmfzrshId());
            mmap.put("approvename", obj.getXmfzrsh());
        }
        return prefix + "/deptcheck";
    }
    
    /**
     * 项目董事
     * @return
     */
    @GetMapping("/director")
    public String director3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
            
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            mmap.put("approveid", obj.getHjshId());
            mmap.put("approvename", obj.getHjsh());
        }
        return prefix + "/directorcheck";
    }
    
    /**
     * 会计
     * @return
     */
    @GetMapping("/hj")
    public String hj4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
            
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(obj.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel5id());
            mmap.put("approvename", configlis.get(0).getLevel5name()); 
        }
        return prefix + "/hjcheck";
    }
    
    /**
     * 总经理
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/zjl")
    public String zjl5(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
            
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(obj.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel6id());
            mmap.put("approvename", configlis.get(0).getLevel6name());            
        }
        return prefix + "/zjlcheck";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	SjContract obj = sjContractService.selectSjContractById(Long.parseLong(p.getBusinessKey().toString()));
        	if(obj.getZilwj()!=null && !obj.getZilwj().equals("")){
            	String[] zlis = obj.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	mmap.put("zilwjvo", file);
            }else{
            	mmap.put("zilwjvo", "");
            }
        	mmap.put("obj", obj);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
        }
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/modifycheck";
    }
    
    //------------------
    @GetMapping("/p4contract/{id}")
    public String p4contract(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailContract";
    }

    /**
     * 查询费用核销列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(SjContract sjContract){
    	startPage();
        sjContract.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	sjContract.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		sjContract.setOwnerId(getUserId());
            }
        }
        //----
        List<SjContract> list = sjContractService.selectSjContractList(sjContract);
        return getDataTable(list);
    }
    
    /**
     * 新增
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
        GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        mmap.put("orderNo", ReturnUUID.getOrderNo("HT"));
        
        GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return prefix + "/add4p";
    }
    
    /**
     * 修改
     */
    @GetMapping("/edit4p/{id}")
    public String edit4p(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        mmap.put("sjContract", sjContract);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
        mmap.put("zilwjvo", sjContract.getZilwj());
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	sjContract.setZilwj(file);
        }
        return prefix + "/edit";
    }
    
    //--------------
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @GetMapping("/editZsht/{id}")
    public String editZsht(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        return prefix + "/editZsht";
    }
    
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/editZshtSubmit")
    @ResponseBody
    public AjaxResult editZshtSubmit(SjContract sjContract){
    	sjContract.setStatus(5);
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract(sjContract));
    }
    
    //----供应商合同
    @RequiresPermissions("system:contract:view")
    @GetMapping("/supplycontract")
    public String supplycontract(){
        return prefix + "/supplycontract";
    }
    
    /**
     * 查询列表
     */
    @RequiresPermissions("system:contract:list")
    @PostMapping("/supplylist")
    @ResponseBody
    public TableDataInfo supplylist(SjContract sjContract){
        startPage();
        sjContract.setDeleteFlag(1);
        sjContract.setContractType(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	sjContract.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		sjContract.setOwnerId(getUserId());
            }
        }
        //----
        List<SjContract> list = sjContractService.selectSjContractList(sjContract);
        return getDataTable(list);
    }


    /**
     * 导出
     */
    @RequiresPermissions("system:contract:export")
    @Log(title = "合同审批", businessType = BusinessType.EXPORT)
    @PostMapping("/supplyexport")
    @ResponseBody
    public AjaxResult supplyexport(SjContract sjContract)
    {
    	sjContract.setDeleteFlag(1);
    	sjContract.setContractType(1);
        List<SjContract> list = sjContractService.selectSjContractList(sjContract);
        ExcelUtil<SjContract> util = new ExcelUtil<SjContract>(SjContract.class);
        return util.exportExcel(list, "项目主合同数据");
    }
    
    /**
     * 新增
     */
    @RequiresPermissions("system:contract:add")
    @GetMapping("/supplyadd")
    public String supplyadd(ModelMap mmap){
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
        GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        gcProject.setStatus(3);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        mmap.put("orderNo", ReturnUUID.getOrderNo("SY"));
        
        mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/supplyadd";
    }


    /**
     * 新增
     */
    @RequiresPermissions("system:contract:add")
    @Log(title = "合同审批", businessType = BusinessType.INSERT)
    @PostMapping("/supplyadd")
    @ResponseBody
    public synchronized AjaxResult supplyaddSave(SjContract sjContract) {
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());
    		
    		GcSupply gcSupply = gcSupplyService.selectGcSupplyById(Long.parseLong(sjContract.getCustomerId().toString()));
    		sjContract.setCustomerName(gcSupply.getName());
    	}
    	
    	String htje = sjContract.getHtje().toString();
    	sjContract.setContractType(1);//供应商合同
    	sjContract.setHtjedx(ConvertUpMoneyUtil.toChinese(htje));
    	sjContract.setOwner(getLoginName());
    	sjContract.setStatus(1);
    	sjContract.setDeleteFlag(1);
    	sjContract.setCreateAt(new Date());
    	sjContract.setCreateUser(getLoginName());
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.insertSjContract4Save(sjContract));
    }
    
    /**
     * 提交
     */
    @RequiresPermissions("system:contract:add")
    @Log(title = "合同审批", businessType = BusinessType.INSERT)
    @PostMapping("/supplyaddSubmit")
    @ResponseBody
    public AjaxResult supplyaddSubmit(SjContract sjContract) {
    	
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());

    		GcSupply gcSupply = gcSupplyService.selectGcSupplyById(Long.parseLong(sjContract.getCustomerId().toString()));
    		sjContract.setCustomerName(gcSupply.getName());
    	}
    	
    	String htje = sjContract.getHtje().toString();
    	sjContract.setContractType(1);//供应商合同
    	sjContract.setHtjedx(ConvertUpMoneyUtil.toChinese(htje));
    	sjContract.setOwner(getLoginName());
    	sjContract.setStatus(1);
    	sjContract.setDeleteFlag(1);
    	sjContract.setCreateAt(new Date());
    	sjContract.setCreateUser(getLoginName());
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.insertSjContract(sjContract));
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @GetMapping("/supplyedit/{id}")
    public String supplyedit(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        gcProject.setStatus(3);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
      //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/supplyedit";
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/supplyedit")
    @ResponseBody
    public AjaxResult supplyeditSave(SjContract sjContract){
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());

    		GcSupply gcSupply = gcSupplyService.selectGcSupplyById(Long.parseLong(sjContract.getCustomerId().toString()));
    		sjContract.setCustomerName(gcSupply.getName());
    	}
    	
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract4Save(sjContract));
    }
    
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/supplyeditSubmit")
    @ResponseBody
    public AjaxResult supplyeditSubmit(SjContract sjContract){
    	GcProject gcProject = gcProjectService.selectGcProjectById(sjContract.getGcProjectId());
    	if(gcProject != null){
    		sjContract.setGcProjectId(gcProject.getId());
    		sjContract.setProjectName(gcProject.getName());

    		GcSupply gcSupply = gcSupplyService.selectGcSupplyById(Long.parseLong(sjContract.getCustomerId().toString()));
    		sjContract.setCustomerName(gcSupply.getName());
    	}
    	
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract(sjContract));
    }
    
    /**
     * 详情
     */
    @GetMapping("/supplydetail/{id}")
    public String supplydetail(@PathVariable("id") Long id, ModelMap mmap){
        SjContract sjContract = sjContractService.selectSjContractById(id);
        mmap.put("sjContract", sjContract);
        return prefix + "/supplydetail";
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:contract:remove")
    @Log(title = "合同审批", businessType = BusinessType.DELETE)
    @PostMapping( "/supplyremove")
    @ResponseBody
    public AjaxResult supplyremove(String ids){
        return toAjax(sjContractService.deleteSjContractByIds(ids));
    }
    
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:edit")
    @GetMapping("/supplyeditZsht/{id}")
    public String supplyeditZsht(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        return prefix + "/supplyeditZsht";
    }
    
    @RequiresPermissions("system:contract:edit")
    @Log(title = "合同审批", businessType = BusinessType.UPDATE)
    @PostMapping("/supplyeditZshtSubmit")
    @ResponseBody
    public AjaxResult supplyeditZshtSubmit(SjContract sjContract){
    	sjContract.setStatus(5);
    	sjContract.setUpdateAt(new Date());
    	sjContract.setUpdateUser(getLoginName());
        return toAjax(sjContractService.updateSjContract(sjContract));
    }
    
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:againedit")
    @GetMapping("/againedit/{id}")
    public String againedit(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList4Approve(new SysUser()));
        
        //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
        //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/edit";
    }
    
    /**
     * 修改
     */
    @RequiresPermissions("system:contract:againedit")
    @GetMapping("/supplyagainedit/{id}")
    public String supplyagainedit(@PathVariable("id") Long id, ModelMap mmap) {
        SjContract sjContract = sjContractService.selectSjContractById(id);
        
        if(sjContract.getZilwj()!=null && !sjContract.getZilwj().equals("")){
        	String[] zlis = sjContract.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("zilwjvo", file);
        }else{
        	mmap.put("zilwjvo", "");
        }
        
        mmap.put("sjContract", sjContract);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	GcProject gcProject = new GcProject();
        gcProject.setDeleteFlag(1);
        List<GcProject> gplis = gcProjectService.selectGcProjectList(gcProject);
        mmap.put("gplis", gplis);
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
      //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        return prefix + "/supplyedit";
    }
}